conf = require("../config")
if not conf.dap.enabled then return {} end

local daps = {
  'python',
}

local set_dap_breakpoint_style = function ()
  local dap_breakpoint_color = {
    breakpoint = {
      ctermbg=0,
      fg='#FF5555',
    },
    logpoing = {
      ctermbg=0,
      fg='#61afef',
      bg='#31353f',
    },
    stopped = {
      ctermbg=0,
      fg='#98c379',
    },
    stopline = {
      ctermbg=0,
      bg='#5e3a28',
    }
  }

  vim.api.nvim_set_hl(0, 'DapBreakpoint', dap_breakpoint_color.breakpoint)
  vim.api.nvim_set_hl(0, 'DapLogPoint', dap_breakpoint_color.logpoing)
  vim.api.nvim_set_hl(0, 'DapStopped', dap_breakpoint_color.stopped)
  vim.api.nvim_set_hl(0, 'DapStopLine', dap_breakpoint_color.stopline)

  local dap_breakpoint = {
    error = {
      text = " ",
      texthl = "DapBreakpoint",
    },
    condition = {
      text = ' ',
      texthl = 'DapBreakpoint',
      linehl = 'DapBreakpoint',
      numhl = 'DapBreakpoint',
    },
    rejected = {
      text = " ",
      texthl = "DapBreakpoint",
      linehl = "DapBreakpoint",
      numhl = "DapBreakpoint",
    },
    logpoint = {
      text = ' ',
      texthl = 'DapLogPoint',
      linehl = 'DapLogPoint',
      numhl = 'DapLogPoint',
    },
    stopped = {
      text = ' ',
      texthl = 'DapStopped',
      linehl = 'DapStopLine',
      numhl = 'DapStopped',
    },
  }

  vim.fn.sign_define('DapBreakpoint', dap_breakpoint.error)
  vim.fn.sign_define('DapBreakpointCondition', dap_breakpoint.condition)
  vim.fn.sign_define('DapBreakpointRejected', dap_breakpoint.rejected)
  vim.fn.sign_define('DapLogPoint', dap_breakpoint.logpoint)
  vim.fn.sign_define('DapStopped', dap_breakpoint.stopped)
end

local is_dap_window_open = function()
  local windows = require "dapui.windows"
  local is_window_open = false
  for i = 1, #windows.layouts, 1 do
    if windows.layouts[i]:is_open() then is_window_open = true end
  end
  return is_window_open
end

local close_all_window = function()
  local windows = require "dapui.windows"
  for i = 1, #windows.layouts, 1 do
    windows.layouts[i]:close()
  end
end

local choose_dap_element = function()
  vim.ui.select({
    "default",
    "console",
    "repl",
    "stacks, breakpoints, watches and scopes",
  }, { prompt = "Select Dap Layout", default = "default" },
  function(select)
    if not select then return end
    if is_dap_window_open() then close_all_window() end
    if select == "default" then
      require("dapui").open { layout = 1, reset = true }
      require("dapui").open { layout = 4, reset = true }
    elseif select == "console" then
      require("dapui").open { layout = 2, reset = true }
    elseif select == "repl" then
      require("dapui").open { layout = 3, reset = true }
    else
      require("dapui").open { layout = 1, reset = true }
    end
  end)
end

return {
  {
    "mfussenegger/nvim-dap",
    lazy = true,
    keys = {
      {",dab", function () require('dap').toggle_breakpoint() end, "n", desc = "toggle breakpoint"},
      {",dao", function () require('dap').step_over() end , "n", desc = "step over"},
      {",dai", function () require('dap').step_into() end, "n", desc = "step into"},
      {",dat", function () require('dap').step_out() end, "n", desc = "step out"},
      {",dac", function () require('dap').continue() end, "n", desc = "dap: continue"},
      {",daq", function () require('dap').terminate() end, "n", desc = "dap: terminate"},
      {",das", function () require('dap').run_to_cursor() end, "n", desc = "run to cursor"},
      {",dah", function () require('dap.ui.widgets').hover() end, "n", desc = "View the value for the expression"},
      -- {",dau", function () require('dapui').toggle() end, "n", desc = "toggle dapui"},
      {",dau", function () choose_dap_element() end, "n", desc = "Toggle Tray Debugger UI and reset layout"},
      {",dad", function () close_all_window() end, "n", desc = "Close all windows"},
    },
    dependencies = {
      {
        "jay-babu/mason-nvim-dap.nvim",
        config = function ()
          require("mason-nvim-dap").setup({
            ensure_installed = daps
          })
        end
      },
      {
        "theHamsta/nvim-dap-virtual-text",
        -- event = "VeryLazy",
        opts = {
          commented = true,
          enabled = true,
          enabled_commands = true,
          only_first_definition = true,
          clear_on_continue = true,
          highlight_changed_variables = true,
          all_frames = false,
          virt_lines = true,
          show_stop_reason = true,
        },
      },
      {
        "rcarriga/nvim-dap-ui",
        dependencies = {
          "nvim-neotest/nvim-nio",
        },
        opts = {
          layouts = {
            {
              elements = {
                { id = "scopes", size = 0.25 },
                { id = "breakpoints", size = 0.25 },
                { id = "stacks", size = 0.25 },
                { id = "watches", size = 0.25 },
              },
              size = 40,
              position = "left", -- Can be "bottom" or "top"
            },
            {
              elements = {
                { id = "console", size = 1 },
              },
              size = 10,
              position = "bottom", -- Can be "bottom" or "top"
            },
            {
              elements = {
                { id = "repl", size = 1 },
              },
              size = 10,
              position = "bottom", -- Can be "bottom" or "top"
            },
            {
              elements = {
                { id = "repl", size = 0.5 },
                { id = "console", size = 0.5 },
              },
              size = 10,
              position = "bottom", -- Can be "bottom" or "top"
            },
          },
          render = {
            max_type_length = 100, -- Can be integer or nil.
            max_value_lines = 100, -- Can be integer or nil.
            indent = 1,
          },
        },
        config = function (_, opts)
          require("dapui").setup(opts)
        end
      },
      {
        "mfussenegger/nvim-dap-python",
        lazy = true,
        -- ft = "python",
        config = function (_, opts)
          -- local path = ""
          -- local file_path = "~/.config/nvim/machine_specific.lua"
          -- if vim.fn.filereadable(file_path) == 1 then
          --   path = 
          -- end
          require("dap-python").setup("python")
        end
      },
    },
    config = function ()
      local dap = require('dap')

      -- set style of debugging
      set_dap_breakpoint_style()

      require("../dap/python").config()
      require("../dap/c").config()
    end
  },
}
